package com.st.mongodb;

import com.mongodb.Block;
import com.mongodb.MongoClient;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;

import org.bson.Document;
import org.bson.codecs.configuration.CodecRegistries;
import org.bson.codecs.configuration.CodecRegistry;

/**
 * @author wuxianwei on 15/11/26.
 */
//TODO 探究code的写法 加快Java反编译的速度
public class CodecDemo {
  public static void main(String[] args) {

    String connectionString = "localhost";
    MongoClient client = new MongoClient(connectionString);
    MongoDatabase database = client.getDatabase("sentidb");
    String table = "test";
    database.getCollection(table).insertOne(new Document("mid","1234").append("text","English")
            .append("retText", "中文"));

    MongoCollection<Document> dc = database.getCollection(table);
    CodecRegistry customCodec = CodecRegistries.fromCodecs(new SentiStatusCodec());
    CodecRegistry customCodecs = CodecRegistries.fromRegistries(customCodec, dc.getCodecRegistry());

    MongoCollection<SentiStatus> sentiStatusTable = database.getCollection(table, SentiStatus.class)
            .withCodecRegistry(customCodecs);


    FindIterable<SentiStatus> findIterable =  sentiStatusTable.find();
    findIterable.forEach((Block<SentiStatus>) sentiStatus -> {
      System.out.println(sentiStatus.getMid()+">"+sentiStatus.getText()+">"+sentiStatus.getRetText());}
    );
  }
}
